
using System;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Tama_Chan.GameEngine_ns;
using Tama_Chan.Utils;


namespace Tama_Chan.Entities_ns
{
   public class PowerPellet : GameEntity
   {
      private PowerPelletState state;
      public const int MaxFoodCount = 15;
      public int EatenFoodCount { get; set; }
      public readonly int[] EnemyScoreValues = new[] { 200, 400, 800, 1600 };
      public int EnemiesEaten{ get; set; }


      public PowerPellet(GameEngine gameEngine)
      {
         GameEngine = gameEngine;
         Texture = gameEngine.PowerPelletTexture;
         SoundOne = GameEngine.ChaseSoundEffect.CreateInstance();
         SoundOne.IsLooped = true;
         ColorArray = new[] { Color.Turquoise, Color.Violet, Color.Fuchsia, Color.DeepPink };
         ScaleArray = new[] { new Vector2(0.75f), new Vector2(1f), new Vector2(1.25f), new Vector2(1f) };
         IntervalTwo = GeneralUtils.GetSecondsAsTicks(0.1f); // Color change
         IntervalThree = GeneralUtils.GetSecondsAsTicks(7); // Attack length
         LifeTime = TimeSpan.TicksPerSecond * 10;
         State = PowerPelletState.Waiting;
         Rotation = 0f;
         RotationRate = MathHelper.TwoPi;
			// ToDo: set Bounds in constructor
         Origin = new Vector2(Texture.Width / 2f, Texture.Height / 2f);
      	ScoreValue = 50;
      }


      public override void Update(GameTime gameTime)
      {
         if(State == PowerPelletState.Waiting)
         {
            if(EatenFoodCount >= MaxFoodCount)
            {
               State = PowerPelletState.Visible;
            }
         }
         else if(State == PowerPelletState.Visible)
         {
            // Rotation
            float elapsedSeconds = GeneralUtils.GetSecondsFromTicks(gameTime.ElapsedGameTime.Ticks);
            float rotationDelta = RotationRate * elapsedSeconds;
            float updatedRotation = Rotation + rotationDelta;
            Rotation = MathHelper.WrapAngle(updatedRotation);

            // Color change
            AccumulatedTicksTwo += gameTime.ElapsedGameTime.Ticks;
            if(AccumulatedTicksTwo > IntervalTwo)
            {
               SetAppearance();
               AccumulatedTicksTwo -= IntervalTwo;
            }

            // Lifetime
            AccumulatedTicksOne += gameTime.ElapsedGameTime.Ticks;
            if(AccumulatedTicksOne > LifeTime)
            {
               State = PowerPelletState.Waiting;
            }
         }
         else // Attacking
         {
            AccumulatedTicksThree += gameTime.ElapsedGameTime.Ticks;
            if(AccumulatedTicksThree > IntervalThree)
            {
               State = PowerPelletState.Waiting;
            }
         }

         base.Update(gameTime);
      }


      public override void Draw()
      {
         if(State == PowerPelletState.Visible)
         {
            GameEngine.Game1.SpriteBatch.Draw(Texture, Position, null, Color, Rotation, Origin, Scale, SpriteEffects.None, Depth);
         }

         base.Draw();
      }


      public PowerPelletState State
      {
         get
         {
            return state;
         }
         set
         {
            state = value;
            switch(value)
            {
               case PowerPelletState.Waiting:
                  ScaleArrayCurrentIndex = 0;
                  AccumulatedTicksTwo = 0;
                  EatenFoodCount = 0;
                  EnemiesEaten = 0;
                  ColorArrayCurrentIndex = 0;
                  SetAppearance();
                  if(SoundOne.State == SoundState.Playing)
                  {
                     SoundOne.Stop();
                  }
                  int right = GameEngine.Game1.GameBounds.Right - 80 - Bounds.Width;
                  int left = GameEngine.Game1.GameBounds.Left + 80;
                  int top = GameEngine.Game1.GameBounds.Top + 80;
                  int bottom = GameEngine.Game1.GameBounds.Bottom - 80 - Bounds.Height;
                  Position = new Vector2(Random.Next(left, right), Random.Next(top, bottom));
                  // Restore enemy colors
                  for(int i = 0; i < GameEngine.EnemyList.Count; i++)
                  {
                     GameEngine.EnemyList[i].Color = Enemy.NormalColor;
                  }
                  break;
               case PowerPelletState.Visible:
                  AccumulatedTicksOne = 0;
                  break;
               case PowerPelletState.Attacking:
                  AccumulatedTicksThree = 0;
                  SoundOne.Play();
                  // Turn all enemies pale
                  for(int i = 0; i < GameEngine.EnemyList.Count; i++)
                  {
                     GameEngine.EnemyList[i].Color = Enemy.FleeingColor;
                  }
                  break;
               default:
                  throw new Exception("Invalid PowerPelletState");
            }
         }
      }


      private void SetAppearance()
      {
         if(ColorArrayCurrentIndex == ColorArray.Count())
         {
            ColorArrayCurrentIndex = 0;
         }
         Color = ColorArray[ColorArrayCurrentIndex++];

         if(ScaleArrayCurrentIndex == ScaleArray.Count())
         {
            ScaleArrayCurrentIndex = 0;
         }
         Scale = ScaleArray[ScaleArrayCurrentIndex++];
      }

   }
}
